package com.android.RssReader.Screen;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.RssReader.R;
import com.android.RssReader.RssReaderApplication;
import com.android.RssReader.Adapter.NewsItemAdapter;
import com.android.RssReader.Service.ScreenService;
import com.android.RssReader.Service.ServiceManager;
import com.android.RssReader.Utils.SecBase64;

public class ScreenSearch extends Screen{
	
    private final ScreenService screenService;
	
	public ScreenSearch() {
		super(SCREEN_TYPE.SCREEN_SEARCH_T, ScreenSearch.class.getCanonicalName());
		screenService = ServiceManager.getScreenservice();
	}
	private static final String[] m={"标题","摘要","内容"};   
	private ListView mListView;
	private Button searchBtn;
	private int select = 0;
	private EditText searchTv;
	private Spinner searchSpinner;
	private NewsItemAdapter mAdapter;
	private ArrayAdapter<String> adapter;   
	private Cursor c ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		RssReaderApplication.LogD(ScreenFavorites.class, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_search);
		searchSpinner = (Spinner) findViewById(R.id.search_spinner);   
		mListView = (ListView) findViewById(R.id.search_list);
		searchBtn = (Button) findViewById(R.id.search_btn);
		searchTv = (EditText) findViewById(R.id.search_text);
		searchBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				String s = searchTv.getText().toString();
				if(s.equals("")){
					Toast.makeText(ScreenSearch.this, "搜索条件不能为空", Toast.LENGTH_SHORT).show(); 
				} else {
					Toast.makeText(ScreenSearch.this, SecBase64.encode(s.getBytes()), Toast.LENGTH_SHORT).show(); 
					c = ServiceManager.getNewsservice().getMediaDB().queryCountLocalNews(select, SecBase64.encode(s.getBytes()));
					if (c != null) {
						Toast.makeText(ScreenSearch.this, "size:" + c.getCount(), Toast.LENGTH_SHORT).show(); 
						mAdapter = new NewsItemAdapter(ScreenSearch.this, c);
						mListView.setAdapter(mAdapter);
						startManagingCursor(c);
					}
				}
			}});
		mListView.setOnItemClickListener(mItemClickListener);
		
		//将可选内容与ArrayAdapter连接起来   
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);   
           
        //设置下拉列表的风格   
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
           
        //将adapter 添加到spinner中   
        searchSpinner.setAdapter(adapter);   
           
        //添加事件Spinner事件监听     
        searchSpinner.setOnItemSelectedListener(new SpinnerSelectedListener());   
           
        //设置默认值   
        searchSpinner.setVisibility(View.VISIBLE);   

        
        
	}
	
	//使用数组形式操作   
    class SpinnerSelectedListener implements OnItemSelectedListener{   
  
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,   
                long arg3) {   
        	Toast.makeText(ScreenSearch.this, m[arg2], Toast.LENGTH_SHORT).show(); 
        	select = arg2;
        }   
  
        public void onNothingSelected(AdapterView<?> arg0) {   
        }   
    }   

	
	@Override
	protected void onResume() {
		RssReaderApplication.LogD(ScreenFavorites.class, "onResume");
		super.onResume();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		RssReaderApplication.LogD(ScreenFavorites.class, "onNewIntent");
		super.onNewIntent(intent);
	}
	
	private OnItemClickListener mItemClickListener = new OnItemClickListener(){

		@Override
		public void onItemClick(AdapterView<?> adapterView, View view, int position,
				long _id) {
			ScreenArgs args = (ScreenArgs) view.getTag();
			args.putExtra("item_id", _id);
			screenService.show(ScreenNewsContent.class, null , args, true);
		}
	};
	
}
